$(function(){
   var
       oBigerImg   = $ ('#bigerImg'),
       oZhuti      = $ ('#zhuti'),
       oClose      = $ ('#bigerImg .close'),
       aBigListLI = $ ('.bigList li'),
       aSmallList = $ ('.smallList li'),
       oBtnListLeft   = $ ('.btn-list .bt1'),
       oBtnListRight   = $ ('.btn-list .bt2'),
       len=0;
    aSmallList.click(function(){
        len = $(this).index();
        aBigListLI.css({display:'none'}).eq(len).css({display:'block'})
    });
    aBigListLI.click(function(){
        oZhuti.css({display:'none'});
        oBigerImg.css({display:'block'})
    });
    oClose.click(function(){
        oBigerImg.css({display:'none'});
        oZhuti.css({display:'block'})
    })

    //给right添加点击按钮
    oBtnListRight.click(function(){
        len++;
        aBigListLI.css({display:'none'}).eq(len).css({display:'block'});
        if(len>0){
            oBtnListLeft.css({display:'block'})
        }
        if(len==3){
            oBtnListRight.css({display:'none'});
        }
    });
    //给left添加点击按钮
    oBtnListLeft.click(function(){
        len--;
        aBigListLI.css({display:'none'}).eq(len).css({display:'block'});
        if(len<3){
            oBtnListRight.css({display:'block'})
        }
        if(len==0){
            oBtnListLeft.css({display:'none'});
        }
    })

    var oLgoodsSize=null;
    //biger img
    var
        oBigerListLi   = $ ('.bigerList li'),
        oSmallerListLi = $ ('.smallerList li'),
        aGoodSizeLi  = $ ('.ul-size li'),
        aGoodSizeLiA  = $ ('.ul-size a');
    oSmallerListLi.click(function(){
        var len1 = $(this).index();
        oBigerListLi.css({display:'none'}).eq(len1).css({display:'block'})
    });

    aGoodSizeLi.mouseenter(function(){
        var liIndex =$(this).index();
        aGoodSizeLi.removeClass('back').eq(liIndex).addClass('back')
        oLgoodsSize=aGoodSizeLiA.eq(liIndex).html()
    })

    var
        oSp2A        = $('.goods-explan .sp2 a'),
        oGoodsExplan =  $('#sweater .goods-explan'),
        oGoodsBtn    = true;
    oSp2A.click(function(){
        if(oGoodsBtn==true){
            oGoodsExplan.animate({height:100},function(){
                oSp2A.html('收缩');
                oSp2A.css({background:'url(../images/icon_arrows_bottom2.gif) no-repeat 28px center'});
                oGoodsBtn=false;
            })
        }

    });
    oSp2A.click(function(){
        if(oGoodsBtn==false){
            oGoodsExplan.animate({height:60},function(){
                oSp2A.css({background:'url(../images/icon_arrows_right.gif) no-repeat right center'});
                oSp2A.html('展开');
                oGoodsBtn=true;
            })
        }
    });

    var
        oAddsp1 = $ ('.add .sp1 a'),
        oAddsp2 = $ ('.add .sp2'),
        oAddsp3 = $ ('.add .sp3 a'),
        oAdd    =1,
        oAddBtn = true,
        oSubBtn = true,
        aGoodsNumber=0;

    oAddsp1.click(function(){
        oAdd--;
        if(oAdd<=1){
            oAddBtn=false;
            oAddsp1.css({color:'#ccc'});
        }
        if(oAddBtn){
            oAddsp2.html(oAdd);
            oAddsp1.css({color:'#000'});
            oAddsp3.css({color:'#000'});
            aGoodsNumber=oAdd;
            goodsNumber = aGoodsNumber;
        }
    })
    oAddsp3.click(function(){
        oAdd++;
        if(oAdd>5){
            oSubBtn=false;
            oAddsp3.css({color:'#ccc'})
        }
        if(oSubBtn){
            oAddsp2.html(oAdd);
            oAddsp1.css({color:'#000'});
            oAddsp3.css({color:'#000'});
            aGoodsNumber=oAdd;
            goodsNumber = aGoodsNumber;
        }
    })


    var
        spantime1=$('.daojishi .cont .em1'),
        spantime2=$('.daojishi .cont .em2'),
        spantime3=$('.daojishi .cont .em3'),
        spantime4=$('.daojishi .cont .em4');
    function timer(){
        var time = new Date();
        var time1 = new Date("2016/10/01");
        var cha = time1-time;
        var day = parseInt(cha/1000/60/60/24);
        var hour = parseInt(cha/1000/60/60%24);
        var minu = parseInt(cha/1000/60%60);
        var secon = parseInt(cha/1000%60);
        if(day>=10){
            day = day;
        }else{
            day = "0"+day;
        }
        if(hour>=10){
            hour = hour;
        }else{
            hour = "0"+hour;
        }
        if(minu>=10){
            minu = minu;
        }else{
            minu = "0"+minu;
        }
        if(secon>=10){
            secon = secon;
        }else{
            secon = "0"+secon;
        }
        spantime1.html(day);
        spantime2.html(hour);
        spantime3.html(minu);
        spantime4.html(secon);
    }
    setInterval(timer,1000);




    //购物车下单
    var
        oGoodsBotton = $ ('.good-button'),
        oNavRight  = $ ('.nav-right .good-number a'),
        oGoodPrice = $ ('.good-price a'),
        goodsNumber = 0;
    oNavRight.html(aGoodsNumber);
    oGoodPrice.html('￥.00');
        oGoodsBotton.click(function(){
            /*div class="good-button clear" goods-card="SP3-112-00596" goods_PinPai="SARA PHILLIPS" goods_type="黑白拼色条纹圆领长袖羊绒连衣裙" goods_price="￥1,198" goods-src="../images/yf11.jpg">*/
            //存储的商品信息：商品品牌、商品名称、商品价格，商品图片地址
            var
                iGoodsType 	= $(this).attr('goods_type'),
                sGoodsCard  = $(this).attr('goods-card'),
                sgoodsPinPai= $(this).attr('goods_PinPai'),
                iGoodsPrice = $(this).attr('goods_price'),
                sGoodsSrc 	= $(this).attr('goods-src');

            var
                sGoods = getCookie('goods'),
                bBtn = true;


            if(sGoods === undefined ) {
                var aGoods= [];
            } else {
                var aGoods=JSON.parse(sGoods);   // 字符串转换成数组
            }

            for(var i =0; i < aGoods.length; i++){
                if(aGoods[i].goodscard == sGoodsCard && aGoods[i].goodSize == oLgoodsSize ){
                    aGoods[i].goodsNum++;
                    bBtn = false;
                }
            }
            if(bBtn) {
                var oGoods = {
                    goodscard: 	sGoodsCard,
                    goodsPinpai: sgoodsPinPai,
                    goodsPrice: iGoodsPrice,
                    goodsSrc: 	sGoodsSrc,
                    goodsNum: 	aGoodsNumber,
                    goodsType:  iGoodsType,
                    goodSize  :  oLgoodsSize
                };
                aGoods.push(oGoods);
            }
            // 写入cookie
            setCookie('goods', JSON.stringify(aGoods), 7, '/');   //数组转化成字符串
            oNavRight.html(aGoodsNumber);
            oGoodPrice.html('￥'+goodsNumber*parseInt(iGoodsPrice)+'.00');
        })
})